import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_category.dart';
import 'package:flutter_learn_vi/src/bean/bean_video.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class VideoStudyMainPage extends StatefulWidget {
  @override
  _VideoStudyMainPageState createState() => _VideoStudyMainPageState();
}

class _VideoStudyMainPageState extends State<VideoStudyMainPage>
    with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  ///页面保持状态
  @override
  bool get wantKeepAlive => true;

  //推荐模拟数据
  List<VideoModel> videoList = [];

  //控制器
  TabController _tabController;

  //Tab集合
  List<Tab> tabs = <Tab>[];

  //主体页面的集合
  List<Widget> bodyList = [];

  String title;

  @override
  void initState() {
    super.initState();

    title = "学习";

    getCategorys();
  }

  List<CategoryBean> _categoryList = [];

  Future<bool> getCategorys() async {
    Map<String, dynamic> map = Map();
    //post请求发送
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.newstype,
      queryParameters: map,
    );
    if (responseInfo.success) {
      List list = responseInfo.data;
      _categoryList = [];
      list.forEach((element) {
        CategoryBean newsBean = CategoryBean.fromJson(element);
        _categoryList.add(newsBean);
      });
    }

    initView();

    return Future.value(true);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (_tabController == null) {
      return Scaffold(
        appBar: buildAppBar(),
        body: Center(
          child: LoadingWidget(),
        ),
      );
    }
    return Scaffold(
      appBar: buildAppBar(),
      body: RefreshIndicator(
        onRefresh: () {
          return Future.value(true);
        },
        child: Column(
          children: [
            Material(color: Colors.white, child: buildTabBar()),
            Expanded(child: buildTabBarView(context)),
          ],
        ),
      ),
    );
  }

  AppBar buildAppBar() {
    return AppBar(
      centerTitle: true,
      elevation: 0,
      title: Text("$title"),
    );
  }

  TabBar buildTabBar() {
    return TabBar(
      //可以和TabBarView关联使用同一个TabController
      controller: _tabController,
      //子Tab
      tabs: tabs,
      isScrollable: true,
    );
  }

  Widget buildTabBarView(BuildContext context) {
    return TabBarView(
      physics: NeverScrollableScrollPhysics(),
      //联动控制器
      controller: _tabController,
      //所有的子页面
      children: bodyList,
    );
  }

  int pageIndex = 1;

  Future<bool> loadingData() async {
    ResponseInfo responseInfo = await DioUtils.instance
        .getRequest(url: HttpHelper.Video_LIST_URL, queryParameters: {
      "pageIndex": pageIndex,
      "pageSize": 30,
    });

    if (responseInfo.success) {
      List data = responseInfo.data;
      if (data.length == 0) {
        setState(() {});
      } else {
        List<VideoModel> itemList = [];
        data.forEach((element) {
          VideoModel bean = VideoModel.fromMap(element);
          itemList.add(bean);
        });

        if (pageIndex == 1) {
          videoList = itemList;
        } else {
          videoList.addAll(itemList);
        }
        setState(() {});
      }
    } else {
      setState(() {});
    }

    return true;
  }

  void initView() {
    List<CategoryItemBean> itemList = [];
    itemList = _categoryList[1].children;
    //初始化Tab 如新闻资讯类型的APP的分类
    tabs = <Tab>[];
    itemList.forEach((element) {
      tabs.add(Tab(
        text: element.categoryName,
      ));
    });

    //创建模拟页面 如新闻资讯类型的APP的分类列表
    // for (int i = 0; i < tabs.length; i++) {
    //   bodyList.add(
    //     VideoItemRequestPage(
    //       itemBean: itemList[i],
    //       key: ValueKey("$i"),
    //     ),
    //   );
    // }
    //initialIndex初始选中第几个
    //[length] 标签的个数
    //[vsync]动画同步依赖
    _tabController =
        TabController(initialIndex: 0, length: tabs.length, vsync: this);

    setState(() {});
  }
}
